using System;
using System.Diagnostics;
using System.Threading;

namespace AsyncLibrary.Internal
{
  /// <summary><see cref="ThreadingCore{T}"/> + <see cref="IOperation"/>
  ///   + <see cref="SynchronizationContext"/>
  /// </summary>
  internal class OperationCore : ThreadingCore<IOperation>
  {
    private SynchronizationContext _syncContext;

    internal SynchronizationContext SyncContext
    {
      get { return _syncContext; }
      set { _syncContext = value; }
    }

    public OperationCore(string threadName)
      : base(threadName)
    { }

    protected override void Processing(IOperation operation)
    {
      Debug.Assert(_syncContext != null);
      try
      {
        operation.Run(new OperationContext(_syncContext));
      }
      catch (Exception ex)
      {
        throw new Exception("VirtualThread cannot handle exceptions!\n" +
                            "See inner exception for more information", ex);
      }
    }
  }
}